<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestItemFunctions extends PHPUnit_Framework_TestCase {

	function test_item_page_moderator_n_owner() {
		$page = new Page();
		$page->content['owner'] = 'jeff';
		$_SESSION['user_id'] = 'not_jeff';

		$item_funcs = new ItemFunctions();

		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->never())
				->method('add_action');
		$item_funcs->set_toolbox($mock_toolbox);

		$item_funcs->item_page_moderator_n_owner($page);
	}

	function test_item_page_fetch_replies() {
		global $CONFIG;

		$page = new Page();

		$expected_num_posts = 5;
		$expected_offset = 10;
		$expected_page_id = 321;
		$expected_sql = 'sql';
		$expected_sql_result = 'sql result';

		$page->id = $expected_page_id;
		$CONFIG['numposts'] = $expected_num_posts;
		$_GET['offset'] = $expected_offset;

		$functions = new ItemFunctions();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(array(
								array(), 
								"POST.*, POST_BODY.*, M1.nick as nick_author, M2.nick as nick_editor, M1.signature as sig_author, M1.signature_spacer as sig_spacer_author, M1.title as title_author, M1.postcount as pc_author, M1.avatar_mini as mini_author, M1.avatar_maxi as maxi_author"), 
						array(
								"MEMBER AS M1, MEMBER AS M2, POST INNER JOIN POST_BODY ON POST.post_id = POST_BODY.post_id", 
								"DUMMY"), 
						"M1.user_id = POST.author AND M2.user_id = POST.editor AND POST.item_id = {$expected_page_id} AND deleted = 0", 
						"creation ASC", 
						"$expected_offset, $expected_num_posts")
				->will($this->returnValue($expected_sql));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue(null));
		$functions->set_db_accessor($mock_db_access);

		$mock_access = $this->getMock('AccessManager');
		$mock_access->expects($this->at(0))
				->method('has_access')
				->with('MODERATE')
				->will($this->returnValue(false));
		$mock_access->expects($this->at(1))
				->method('has_access')
				->with('ADMIN')
				->will($this->returnValue(true));
		$functions->set_access_manager($mock_access);

		$functions->item_page_fetch_replies($page);

		$this->assertEquals(array(), $page->content['replies']);
	}
}

?>
